<?php

function showRow($arrField)
{
	$length = count($arrField);
		
	foreach($arrField as $index => $value)
	{
		if($index !== $length-1)
		{
			echo $value.",";
		}
		else
		{
			echo $value."\n";
		}

	}
}


if(isset($_POST['Submit']))
{
	$uploaddir = '';
	$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

	//print_r($_FILES); die;
	header("Content-type: application/csv");
	header("Content-Disposition: attachment; filename=file.csv");
	header("Pragma: no-cache");
	header("Expires: 0");
	
	$arrFilterRemoteIP = array(
		'110.44.185.33',
		'110.44.185.38',
		'110.44.185.32',
	);


	$handle = fopen($_FILES['userfile']['tmp_name'], 'r');

	$i = 0;
	
		
	//phần tử trong 1 dòng cách nhau dấu , bỏ vào trong mãng
	while ($data= fgetcsv($handle, 0, ","))

	{

		list($date, $time, $x, $gateway, $ConnectionId, $CallLegDirection,
		$CallLegType, $CallLegConnectTime, $CallLegDisconnectTime,
		$CallLegDisconnectCause, $RemoteGatewayIPAddress,
		$CallingStationId, $CalledStationId, $AcctSessionTime) = $data;
			
		//Tính từ phần tử thứ 2
		if($i!=0)
		{
			
			$arrGateway = explode("=", $gateway);
			if(isset($arrGateway[1]))
			{
				$gateway = $arrGateway[1];
			}


			$arrCallLegType = explode("=", $CallLegType);
			if(isset($arrCallLegType[1]))
			{
				$CallLegType = $arrCallLegType[1];
			}


			$arrCallLegConnectTime = explode("=*",$CallLegConnectTime);
			if(isset($arrCallLegConnectTime[1]))
			{
				$CallLegConnectTime = $arrCallLegConnectTime[1];
			}


			$arrCallLegDisconnectTime = explode("=*", $CallLegDisconnectTime);
			if(isset($arrCallLegDisconnectTime[1]))
			{
				$CallLegDisconnectTime = $arrCallLegDisconnectTime[1];
			}
			
			
			$arrConnectTime = explode(" ", $CallLegConnectTime);
			$arrDisConnectTime = explode(" ",$CallLegDisconnectTime );
			
			//	Tạo đối tượng DatTime		
			$ConnectTime = new DateTime();
			$month =date("m", strtotime($arrConnectTime[3]));
			$ConnectTime->setDate($arrConnectTime[5],$month, $arrConnectTime[4] );
			$tmp = explode(":",$arrConnectTime[0]);
			$ConnectTime->setTime($tmp[0],$tmp[1], $tmp[2]);
			
			//	Tạo đối tưởng dateTime		
			$DisConnectTime = new DateTime();
			$month =date("m", strtotime($arrDisConnectTime[3]));
			$DisConnectTime->setDate($arrDisConnectTime[5], $month, $arrDisConnectTime[4]);
			$tmp = explode(":",$arrDisConnectTime[0]);
			$DisConnectTime->setTime($tmp[0],$tmp[1], $tmp[2]);
			
			//	Trừ thời gian		
			$InterVal = $DisConnectTime->diff($ConnectTime);
			
			//	Chuyển sang giây		
			$seconds = ($InterVal->s)
			         + ($InterVal->i * 60)
			         + ($InterVal->h * 60 * 60)
			         + ($InterVal->d * 60 * 60 * 24)
			         + ($InterVal->m * 60 * 60 * 24 * 30)
			         + ($InterVal->y * 60 * 60 * 24 * 365);
			$InterVal = $seconds;
			unset($ConnectTime);
			unset($DisConnectTime);
			

			$arrCallLegDisconnectCause = explode("=", $CallLegDisconnectCause);
			if(isset($arrCallLegDisconnectCause[1]))
			{
				$CallLegDisconnectCause = $arrCallLegDisconnectCause[1];
			}


			$arrRemoteGatewayIPAddress = explode("=", $RemoteGatewayIPAddress);
			if(isset($arrRemoteGatewayIPAddress[1]))
			{
				$RemoteGatewayIPAddress = $arrRemoteGatewayIPAddress[1];
			}

			if(trim($CallLegType) == 'VoIP' && (strpos($RemoteGatewayIPAddress, '110.44.185')===0) && trim($InterVal) !=0)
			{
				
				$arrField = array(
					$date,
					$time,
					$gateway,
					$CallLegType,
					$InterVal,
					$CallLegDisconnectCause,
					$RemoteGatewayIPAddress,
					$CallingStationId,
					$CalledStationId,
				);
				//echo $date . ',' . $time . ',' . $gateway.','.$CallLegType.','.$CallLegDisconnectCause.','.$RemoteGatewayIPAddress.','.$CallingStationId.','.$CalledStationId,"\n";
					
				showRow($arrField);
			}
			
//			if(isset($a))
//			{
//				die;
//			}
		}
		else
		{
			$arrField = array(
				$date,
				$time,
				$gateway,
				$CallLegType,
				'Duration',
				$CallLegDisconnectCause,
				$RemoteGatewayIPAddress,
				$CallingStationId,
				$CalledStationId,
			);
			showRow($arrField);
		}

		$i++;
	}

	fclose($handle);

}

else
{
	?>

<table width="500" border="0" align="center" cellpadding="0"
	cellspacing="1" bgcolor="#CCCCCC">
	<tr>
		<form method="post" name="form1" id="form1"
			enctype="multipart/form-data" action="<?php $PHP_SELF ?>">Send this
		file: <input name="userfile" type="file" /> <input type="submit"
			name="Submit" value="Send File" /></form>
	</tr>
</table>
	<?php
}